local name = "oLightsUp"
local F = {[-1] = "BankFrame" }
local L
local P = {}
local G = getfenv(0)
local addon = DongleStub('Dongle-Beta0'):New(name)

local buildBagList = function()
	for i=1,12 do
		local name = "ContainerFrame"..i
		local id = G[name]:GetID()

		if(id < 100 and not F[id]) then
			F[id] = name
		end
	end
end

local hi = function(bag, slot, size)
	local id = F[bag]

	if(b ~= -1) then slot = size-slot+1 end
	
	local frame = string.format("%sItem%d", id, slot)
	G[frame.."NormalTexture"]:SetTexture("Interface\\Buttons\\UI-Listbox-Highlight2")
	G[frame.."NormalTexture"]:SetBlendMode("MOD")
	G[frame.."NormalTexture"]:ClearAllPoints()
	G[frame.."NormalTexture"]:SetAllPoints(_G[frame])
	G[frame.."NormalTexture"]:SetVertexColor(0, 1, 1)

	P[frame] = true
end

local uhi = function()
	for frame in pairs(P) do
		G[frame.."NormalTexture"]:SetTexture("Interface\\Buttons\\UI-Quickslot2")
		G[frame.."NormalTexture"]:SetBlendMode("BLEND")
		G[frame.."NormalTexture"]:ClearAllPoints()
		G[frame.."NormalTexture"]:SetPoint("CENTER", G[frame], 0, -1)
		G[frame.."NormalTexture"]:SetVertexColor(1, 1, 1)
		P[frame] = nil
	end
end

local scanBag = function(bag, find, exact)
	local size, link, name = GetContainerNumSlots(bag)

	if(size > 0) then
		for s=1, size do
			link = GetContainerItemLink(bag, s)

			if(link) then
				name = select(3, string.find(link, "%[(.+)%]"))

				if(not exact and name:lower():match(find:lower())) then
					hi(bag, s, size)
				elseif(name == find) then
					hi(bag, s, size)
				end
			end
		end
	end
end

function addon:Initialize()
	G["SLASH_FIND1"] = "/find"
	G.SlashCmdList["FIND"] = function(cmd)
		if(cmd:sub(1,2) == "-e") then
			return self:search(cmd:sub(4), true)
		else
			return self:search(cmd)
		end
	end
end

function addon:Enable()
	self:RegisterEvent("ITEM_LOCK_CHANGED", uhi)
end

function addon:search(find, exact)
	uhi()
	buildBagList()

	self:Print("Searching bags for: "..find)

	for i=-1,10 do
		scanBag(i, find, exact)
	end

end

G[name] = addon
